package flowlight.action;

import flowlight.entity.FlowLight;
import flowlight.entity.FlowLightPic;
import flowlight.service.FliowLightPicService;
import flowlight.service.FlowLightService;
import flowlight.service.UserService;
import flowlight.utils.JsonVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller("flowlightController")
@RequestMapping("flowlight")
public class FlowLightAction {
    @Resource(name = "FlowLightService")
    private FlowLightService flowLightService;
    @Resource(name = "FliowLightPicService")
    private FliowLightPicService fliowLightPicService;
    @Resource(name = "UserService")
    private UserService userService;


    /**
     * 添加留光
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "insertFlowLight",produces="text/html;charset=UTF-8")
    public JsonVo<Integer> insertFlowLight(FlowLight flowLight) {
        JsonVo<Integer> jsonVo = new JsonVo<Integer>();
        flowLightService.insertFlowLight(flowLight);
        int id = flowLight.getId();
        jsonVo.setCode(200);
        jsonVo.setMsg("添加成功");
        jsonVo.setData(id);
        return jsonVo;
    }
    /*
    查询留光
     */
    @ResponseBody
    @RequestMapping("queryAllFlowLight")
    public JsonVo<List<FlowLight>> queryAllFlowLight() {
        JsonVo<List<FlowLight>> jsonVo = new JsonVo<List<FlowLight>>();
        List<FlowLight> list = flowLightService.queryAllFlowLight();
        for (FlowLight flowLight : list) {
            flowLight.setFlowLightPicList(fliowLightPicService.queryFlowLightPic(flowLight.getId()));
            flowLight.setUser( userService.findById(flowLight.getUserId()));
        }
        jsonVo.setMsg("查询成功");
        jsonVo.setCode(200);
        jsonVo.setData(list);
        return jsonVo;
    }

    @ResponseBody
    @RequestMapping("queryMyFlowLight")
    public JsonVo<List<FlowLight>> queryMyFlowLight(Integer userId) {
        JsonVo<List<FlowLight>> jsonVo = new JsonVo<List<FlowLight>>();
        List<FlowLight> list = flowLightService.queryFlowLight(userId);
        for (FlowLight flowLight : list) {
            flowLight.setFlowLightPicList(fliowLightPicService.queryFlowLightPic(flowLight.getId()));
            flowLight.setUser( userService.findById(flowLight.getUserId()));
        }
        jsonVo.setMsg("已经查询成功");
        jsonVo.setCode(200);
        jsonVo.setData(list);
        return jsonVo;
    }

    /**
     * 删除我的留光
     * @param userId
     * @param flowLIghtId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteMyFlowLight")
    public JsonVo<String> deleteFlowLight(Integer userId,Integer flowLIghtId) {
        JsonVo<String> jsonVo = new JsonVo<String>();
        flowLightService.deleteFlowLight(userId,flowLIghtId);
        fliowLightPicService.deleteFlowLightPic(flowLIghtId);
        jsonVo.setCode(200);
        jsonVo.setMsg("删除成功");
        return jsonVo;
    }

    /**
     * 添加留光图片
     * @return
     */
    @ResponseBody
    @RequestMapping("addFlowLightPic")
    public JsonVo<String> insertFlowLightPic(String flowLightId,String picUrl) {
        FlowLightPic flowLightPic = new FlowLightPic();
        int mflowLightId = Integer.parseInt(flowLightId);
        flowLightPic.setFlId(mflowLightId);
        flowLightPic.setPicUrl(picUrl);
        JsonVo<String> jsonVo = new JsonVo<String>();
        fliowLightPicService.insertFlowLightPic(flowLightPic);
        jsonVo.setMsg("添加成功");
        jsonVo.setCode(200);
        return jsonVo;
    }
}
